package pers.liyan.shi.springboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.stereotype.Service;
import pers.liyan.shi.springboot.bean.Department;
import pers.liyan.shi.springboot.mapper.DepartmentMapper;

@Service
@CacheConfig(cacheNames = "dept", cacheManager = "deptCacheManager")
public class DepartmentService {

    @Autowired
    private DepartmentMapper mapper;

    @Qualifier("deptCacheManager")
    @Autowired
    private RedisCacheManager deptCacheManager;

    //使用注解的方式缓存
    //@Cacheable()
    //public Department getDeptById(Integer id) {
    //    return mapper.getDeptById(id);
    //}

    //使用编码的方式进行缓存
    public Department getDeptById(Integer id){
        Department department = mapper.getDeptById(id);
        Cache cache = deptCacheManager.getCache("dept");
        cache.put(id, department);;
        return department;
    }
}
